package com.blic.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.blic.utils.URLUtil;

@Controller
public class AuthController {
	private static Logger logger = LoggerFactory.getLogger(AuthController.class);
	@Value("${appID}")
	private String appID ;
	
	@Value("${weixinAuthUrl}")
	private String weixinAuthUrl ;
	
	@Value("${baseDaominName}")
	private String baseDaominName;
	
	@RequestMapping(value = "/auth")
	public String index(HttpServletRequest request, HttpServletResponse response,String redirectUrl) throws ServletException, IOException {
		StringBuilder url = new StringBuilder();
		
		url.append("redirect:");
		url.append(auth(redirectUrl));
		return url.toString();
	}
	
	  public String auth(String redirectUrl) {
		  //System.out.println("redirectUrl ************"+redirectUrl);
	        StringBuilder url=new StringBuilder();
	        url.append(weixinAuthUrl);
	        url.append("appid=");
	        url.append(appID);
	        url.append("&redirect_uri=");
	        url.append(URLUtil.encode(baseDaominName+redirectUrl));
	        url.append("&response_type=code");
	        url.append("&scope=snsapi_base");
	        url.append("&state=00#wechat_redirect");
	    //    System.out.println("url ************"+url.toString());
	        
	        logger.info("uri:{}",url.toString());
	        return url.toString();
	    }
}
